package com.reserv.controller;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.reserv.action.CommandProcess;
import com.reserv.action.ForwardAction;

public class URICommandController extends HttpServlet {
	
	Map<String, CommandProcess> commandMap = new HashMap<String, CommandProcess>();

	public void init() throws ServletException {
		String commandURI = getInitParameter("CommandURI");
		
		Properties prop = new Properties();
		
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		
		try {
			String realPath = getServletContext().getRealPath(commandURI);
			
			fis = new FileInputStream(realPath);
			bis = new BufferedInputStream(fis);
			
			prop.load(bis);
			
		} catch(IOException e) {
			throw new ServletException();
		} finally {
			try {
				if(bis != null) bis.close();
				if(fis != null) fis.close();
			} catch(IOException ioe) { }
		}
		Iterator<Object> keyIter = prop.keySet().iterator();
		
		while(keyIter.hasNext()) {
			String command = (String) keyIter.next();
			String className = (String) prop.getProperty(command);
			System.out.println("init() - command : " + command + ", className : " + className);
			try {
				Class<?> commandClass = Class.forName(className);
				CommandProcess commandProcess = (CommandProcess) commandClass.newInstance();
				
				commandMap.put(command, commandProcess);
				
			} catch(ClassNotFoundException e) {
				throw new ServletException();
			} catch(InstantiationException e) {
				throw new ServletException();
			} catch(IllegalAccessException e) {
				throw new ServletException();
			}
		}
		System.out.println("URLCommandController - init() - all urlCommands loaded");
	}
	public void doProcess(
			HttpServletRequest request, HttpServletResponse response) 
					throws ServletException, IOException {
		ForwardAction forward = null;
		CommandProcess commandProcess = null;
		String command = request.getRequestURI();
		
		if(command.indexOf(request.getContextPath()) == 0)
			command = command.substring(request.getContextPath().length());
		
		commandProcess = commandMap.get(command);
		
		System.out.println("URLCommandController - doProcess() "
				+ "- command : " + command + ", commandPro : " + commandProcess);
		
		forward = commandProcess.requestProcess(request, response);
		if(forward != null) {			
			if(forward.isRedirect()) {
				response.sendRedirect(forward.getPath());
			} else {
				RequestDispatcher rd = request.getRequestDispatcher(forward.getPath());
				rd.forward(request, response);
			}
		}
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
					throws ServletException, IOException { doProcess(request, response); }
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
					throws ServletException, IOException { doProcess(request, response); }
}